package com.example.myapplication.view;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;
import com.example.myapplication.until.Globals;
import com.example.myapplication.until.ImageUtils;
import com.example.myapplication.until.JBoxUtils;
import com.example.myapplication.vo.Bird;
import com.example.myapplication.vo.Pig;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MainView extends View {
    public Point nowPosition;

    public Bitmap autoImg = null ;
    public Bitmap btn = null ;

    public boolean slide = false;
    public boolean auto  ;
    public int autocount ;

    public Point startNowPosition;
    public Point startPoint;
    public List<Body> landBody = new ArrayList<Body>();

    public BridShotter bridShotter ;

    public Timer timer ;

    public Bird bird;

    // 记录手指的坐标
    public float[] fingerPoint = new float[2];

    // 保存弹弓的相关坐标
    public float[] dangongCenterPoint = new float[2];
    public float[] dangongLeftPoint = new float[2];
    public float[] dangongRightPoint = new float[2];

    // 声明一个标志位, 用来保存当前弹弓的状态
    public boolean isBird = false;

    public MainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        nowPosition = new Point(0, -Globals.SCREEN_HEIGH);

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        autoImg = ImageUtils.getManfulImg() ;
        btn = ImageUtils.getBeginImg() ;
        // 初始化弹弓的坐标
        dangongCenterPoint[0] = Globals.PIECE_WIDTH * 4.25f;
        dangongLeftPoint[0] = Globals.PIECE_WIDTH * 4.125f;
        dangongRightPoint[0] = Globals.PIECE_WIDTH * 4.375f;
        dangongCenterPoint[1] = Globals.PIECE_HEIGH * 8.75f;
        dangongLeftPoint[1] = Globals.PIECE_HEIGH * 8.75f;
        dangongRightPoint[1] = Globals.PIECE_HEIGH * 8.75f;
        MyTouch myTouch = new MyTouch(this) ;
        this.setOnTouchListener(myTouch);
        init();
    }

    public void init() {
        //清空
        Globals.PIGLIST.clear();
        Globals.BIRDLIST.clear();
        // 初始化关卡
        JBoxUtils.init();
        bird=null ;
        bridShotter = new BridShotter();
        bridShotter.setScreen(nowPosition);
        bridShotter.setPb(new Point(Globals.SCREEN_WIDTH/4,Globals.SCREEN_HEIGH*3/4));
        timer = new Timer(30);
        landBody.clear();

        // 这里写死两个固定地面物体
        landBody.add(JBoxUtils.createBox(Globals.SCREEN_WIDTH,
                Globals.PIECE_HEIGH, Globals.SCREEN_WIDTH / 2,
                Globals.PIECE_HEIGH * 11f, 0, JBoxUtils.LAND_TYPE, true));
        landBody.add(JBoxUtils.createBox(Globals.SCREEN_WIDTH,
                Globals.PIECE_HEIGH, Globals.SCREEN_WIDTH * 1.5f,
                Globals.PIECE_HEIGH * 11f, 0, JBoxUtils.LAND_TYPE, true));

        autocount = 0 ;
        //加两面墙
        JBoxUtils.createBox(1,Globals.SCREEN_HEIGH*5,0,Globals.PIECE_HEIGH * 11.5f,0,JBoxUtils.LAND_TYPE,true);
        JBoxUtils.createBox(1,Globals.SCREEN_HEIGH*5,Globals.SCREEN_WIDTH*2,Globals.PIECE_HEIGH * 11.5f,0,JBoxUtils.LAND_TYPE,true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(auto){
            autocount++ ;
            if(autocount%30==0){
                bridShotter.shot();
            }
        }

        JBoxUtils.toDestory();
        Paint paint = new Paint();
        Paint paint2 = new Paint() ;
        paint2.setTextSize(50);
        paint2.setColor(Color.BLUE);

        if (nowPosition != null) {
            // 绘制背景
            canvas.drawBitmap(ImageUtils.getBackground(), nowPosition.x,
                    nowPosition.y, paint) ;
            canvas.drawBitmap(ImageUtils.getBirdImg(Bird.getPreBridIndex()),0,0,paint);

            // 循环绘制地面
            Iterator<Body> iterLand = landBody.iterator();
            while (iterLand.hasNext()) {
                Body b = iterLand.next();
                canvas.drawBitmap(ImageUtils.getLandImg(), b.getPosition().x
                        * Globals.RATE - Globals.SCREEN_WIDTH / 2
                        + nowPosition.x, b.getPosition().y * Globals.RATE
                         + nowPosition.y, paint);
            }

            Bird.drewBirds(canvas,paint,nowPosition);
            Pig.drewPigs(canvas,paint,nowPosition);

            // 绘制弹弓的线
            if (isBird||auto) {
                paint.setColor(Color.RED);

                canvas.drawLine(fingerPoint[0] + nowPosition.x, fingerPoint[1]
                                + nowPosition.y, dangongRightPoint[0] + nowPosition.x,
                        dangongRightPoint[1] + nowPosition.y, paint);

                canvas.drawBitmap(ImageUtils.getBirdImg(Bird.getBridCuerrentIndex()), fingerPoint[0]
                                + nowPosition.x - Globals.PIECE_WIDTH / 2,
                        fingerPoint[1] + nowPosition.y - Globals.PIECE_HEIGH
                                / 2, paint);

                canvas.drawLine(fingerPoint[0] + nowPosition.x, fingerPoint[1]
                                + nowPosition.y, dangongLeftPoint[0] + nowPosition.x,
                        dangongLeftPoint[1] + nowPosition.y, paint);

            }

            // 绘制弹弓图片
            canvas.drawBitmap(ImageUtils.getDangongImg(), Globals.PIECE_WIDTH
                    * 4 + nowPosition.x, Globals.PIECE_HEIGH * 8
                    + nowPosition.y, paint);
            canvas.drawText("score: "+Globals.SCORE,Globals.SCREEN_WIDTH-Globals.PIECE_WIDTH*2,100,paint2);
            paint2.setColor(Color.BLACK);
            paint2.setTextSize(75);
            canvas.drawBitmap(autoImg,0,Globals.SCREEN_HEIGH/2.0f,paint);
            canvas.drawBitmap(btn,Globals.SCREEN_WIDTH-Globals.PIECE_WIDTH,Globals.SCREEN_HEIGH/3.0f,paint);
            paint2.setColor(Color.BLACK);
            paint2.setTextSize(190);
            if(timer.isBegin()){
                canvas.drawText(""+timer.getCurrentLoop(),Globals.SCREEN_WIDTH-Globals.PIECE_WIDTH+35,Globals.SCREEN_HEIGH/3.0f+190,paint2);
            }else{
                btn = ImageUtils.getBeginImg() ;
            }
        }
    }


    class BridShotter{

        private Point pb = null ;
        private Point screen = null ;

        void setPb(Point p){
            pb = p ;
        }

        void setScreen(Point p){
            screen = p ;
        }

        void shot(){
            if(JBoxUtils.world.isLocked()){
                return;
            }
            Bird bird = new Bird(1,Bird.getBridIndex());
            bird.setBody(JBoxUtils.createCircle(10, pb.x
                            - screen.x, pb.y - screen.y,
                    JBoxUtils.BIRD_TYPE, 0, false));

            //向量 给他一个冲量
            Vec2 v1 = new Vec2(
                    -(pb.x - screen.x - dangongCenterPoint[0]) * 25,
                    -(pb.y - screen.y - dangongCenterPoint[1]) * 25) ;

            bird.getBody().applyLinearImpulse(v1,bird.getBody().getWorldCenter());

            Globals.BIRDLIST.add(bird) ;
        }
    }

}

class Timer extends Thread{

    private int loop ;
    private int currentLoop ;
    private boolean begin;

    Timer(int l){
        loop = l ;
        currentLoop = l;
        begin = false ;
    }

    @Override
    public void run() {
        super.run();
        currentLoop = loop ;
        begin = true ;
        while (currentLoop>0){
            try {
                Thread.sleep(1000);
                System.out.println("hahahaha");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            currentLoop-- ;
        }
        begin = false ;
    }

    public int getCurrentLoop() {
        return currentLoop;
    }

    public boolean isBegin() {
        return begin;
    }
}
